import { userService } from '@/api/user';
import { UserDTO } from '@/bean/dto';
import { LoginModel, ObjectResponse } from '@/bean/xhr';
import { ActionContext } from 'vuex';
import { RootState } from '.';
import { LOGIN_ACTION, LOGOUT_ACTION, SET_USER_INFO } from './actiontype';

export default {
  // 用户登录
  async [LOGIN_ACTION](
    { commit }: ActionContext<RootState, RootState>,
    payload: LoginModel
  ): Promise<ObjectResponse<UserDTO>> {
    const res = await userService.login(payload);
    const userInfo = res.data;
    commit(SET_USER_INFO, userInfo);
    return userInfo;
  },
  // 用户登出
  async [LOGOUT_ACTION]({
    commit,
  }: ActionContext<RootState, RootState>): Promise<void> {
    await userService.logout();
    commit(SET_USER_INFO, null);
  },
};
